var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
const mongoose = require('./untils/db');

var usersRouter = require('./routes/users');
var adminRouter = require('./routes/admin');

var app = express();

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/users', usersRouter);
app.use('/admin', adminRouter);

app.use((err, req, res, next) => {
  if (err.name === 'UnauthorizedError') {
    return res.send({
      flag: false,
      status: 401,
      msg: '无效的token',
    })
  }
  res.send({
    flag: false,
    status: 500,
    msg: '未知的错误',
  })
})

module.exports = app;
